package com.youlu.campus.admin.common.controller;

import cn.hutool.core.lang.UUID;
import com.youlu.campus.admin.common.service.QQService;
import com.youlu.campus.admin.utils.IdGenerator;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.component.QiniuConfig;
import com.youlu.campus.component.QiniuUtil;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//@Api(description = "公共接口:文件上传")
@Slf4j
@RestController
public class FileUploadController {

    @Autowired
    private QiniuConfig qiniuConfig;

    @Autowired
    QQService qqService;

    @Autowired
    private QiniuUtil qiniuUtil;

    /**
     * 文件上传上传
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/public/upload-file")
    public QResult updateFile(MultipartFile file) {
        if (file == null) {
            log.error(":>>> 上传的文件空");
            return QResult.fail("请上传文件。");
        }
        try {
            String fileName = file.getOriginalFilename().trim();
            log.info("FileUploadController file upload start file-name:{}", fileName);
            boolean checkName = this.checkFileName(file.getName());
            if (!checkName) {
                log.error(":>>> 文件格式非法");
                return QResult.fail("文件格式非法");
            }
            if ((fileName.endsWith(".png") || fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))
                    && qqService.hasIllegal(file.getResource())) {
                return QResult.fail("图片违规请重新上传。");
            }
            return QResult.success(uploadFile(file));
        } catch (BusinessException e) {
            log.error("<<<<<<<<<<<上传文件失败", e);
            return QResult.fail("上传文件失败");
        } catch (Exception e) {
            log.error("<<<<<<<<<<<上传文件失败", e);
            return QResult.fail("上传文件失败");
        }
    }

    public String uploadFile(MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename().trim();
            log.info("上传文件开始。。。 {}", originalFilename);
            //获取上传源文件扩展名
            String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName = UUID.fastUUID().toString().replace("-", "").toLowerCase() + fileSuffix;
            log.info("文件名:{},新文件名:{},后缀扩展:{}", originalFilename, fileName, fileSuffix);
            //上传到七牛
            qiniuUtil.uploadFile(file.getInputStream(), fileName);
            String url = qiniuConfig.getDomainOfBucket() + "/" + fileName;
            return url;
        } catch (IOException e) {
            log.error("<<<<<<<<<<<上传文件失败:{}", e.getMessage(), e);
            throw new BusinessException("文件上传失败！");
        }
    }

    public boolean checkFileName(String fileName) {
        log.info(":>>> 文件名:{}", fileName);
        if (StringUtils.isBlank(fileName)) {
            return false;
        }
        if (fileName.contains(".txt") || fileName.contains(".html") || fileName.contains(".js") ||
                fileName.contains(".java") || fileName.contains(".jar") || fileName.contains(".htm") ||
                fileName.contains(".mht") || fileName.contains(".asp") || fileName.contains(".jsp") ||
                fileName.contains(".phtml") || fileName.contains(".php3") || fileName.contains(".dat") || fileName.contains(".exe") ||
                fileName.contains(".sh")) {
            log.info(":>>> 文件名:{} 包含非法格式", fileName);
            return false;
        }
        return true;
    }
}
